home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Cheat II / original / cheatrick.cold < prev    next >
Text File  |  1996-01-05  |  4KB  |  204 lines

  1.  // cheatrick.c
  2. // all the trap patch shit
  3.  
  4. #include "cheatrick.h"
  5. #include "cheatWindow.h"
  6. #include "main.h"
  7. #include "cheat.h"
  8. #include "ResRefs.h"
  9. #include "cheat.h"
  10. #include "cheatwich.h"
  11. #include "pascal.h"
  12. #include <string.h>
  13. #include "error.h"
  14. #include "Timer.h"
  15. #include "Processes.h"
  16.  
  17. #define commandkey 0x37
  18. #define optionkey 0x3A
  19. #define controlkey 0x3B
  20.  
  21.     // used for saving and restoring registers
  22. #define setupcode() asm {\
  23.                 movem.l    a0-a5/D0-D7, -(SP)\
  24.             }
  25. #define cleanupcode() asm {\
  26.                 movem.l    (SP)+, a0-a5/D0-D7\
  27.             }
  28.  
  29.  
  30.  
  31. static long oldtrap;
  32. unsigned char km[16];
  33.  
  34.  
  35.  
  36.     // k =  any keyboard scan code, 0-127
  37. static int isPressed(unsigned short k )
  38. {
  39.     return ( ( km[k>>3] >> (k & 7) ) & 1);
  40. }
  41.  
  42. pascal void myGetKeys(KeyMap theKeys)
  43. {
  44.     long oura5;
  45.     GDHandle            SaveGD;
  46.     CGrafPtr            SavePort;
  47.     
  48.     setupcode();
  49.     oura5 = SetCurrentA5();
  50.     
  51.     CallPascal(theKeys, oldtrap);
  52. /*    asm{
  53.         move.l theKeys, km
  54.         }  */
  55.         // my sneaky asm code doesn't seem to work, so . . .
  56.     *((long *)&km[0]) = theKeys[0];
  57.     *((long *)&km[4]) = theKeys[1];
  58.     *((long *)&km[8]) = theKeys[2];
  59.     *((long *)&km[12]) = theKeys[3];
  60.     if (isPressed(controlkey) && isPressed(commandkey) && isPressed(optionkey)) {
  61.             // they've hit the key combo to enter our program
  62. //        GetGWorld(&SavePort, &SaveGD);
  63.         SysBeep(1);
  64.     }
  65.     
  66.     cleanupcode();
  67. }
  68.  
  69. void doInstallTrap(void)
  70. {    
  71.     oldtrap = GetToolTrapAddress(0x176);
  72.     verify(oldtrap);
  73.     SetToolTrapAddress((long) myGetKeys, 0x176);
  74. }
  75.  
  76. void doRemoveTrap(void)
  77. {
  78.     SetToolTrapAddress(oldtrap, 0x176);
  79. }
  80.  
  81.  
  82. //////////////////////////////////////////////////////////
  83. //////////////////////////////////////////////////////////
  84.  
  85. static void
  86. __GetA5(void)
  87. {
  88.     asm {
  89.         bsr.s    @1
  90.         dc.l    0            ;  store A4 here
  91. @1        move.l    (sp)+,a1
  92.     }
  93. }
  94.  
  95. #define RememberA5()    do { __GetA5(); asm { move.l a5,(a1) } } while (0)
  96. #define RecallA5()        do { __GetA5(); asm { move.l (a1),a5 } } while (0)
  97.  
  98.  
  99. pascal void newGetKeys(KeyMap theKeys)
  100. {
  101.     long oura5;
  102.     
  103.     setupcode();
  104.     RecallA5();
  105.     
  106.     CallPascal(theKeys, oldtrap);
  107. /*    asm{
  108.         move.l theKeys, km
  109.         }  */
  110.         // my sneaky asm code doesn't seem to work, so . . .
  111.     *((long *)&km[0]) = theKeys[0];
  112.     *((long *)&km[4]) = theKeys[1];
  113.     *((long *)&km[8]) = theKeys[2];
  114.     *((long *)&km[12]) = theKeys[3];
  115.     if (isPressed(controlkey) && isPressed(commandkey) && isPressed(optionkey)) {
  116.             // they've hit the key combo to enter our program
  117.         if (!cheatwich(oldtrap))
  118.             SysBeep(1);
  119.     }
  120.     
  121.     cleanupcode();
  122. }
  123.  
  124.  
  125. typedef struct {
  126.     struct TMTask atm;
  127.     long tmWakeUp;
  128.     long tmReserved;
  129.     long tmRefCon;
  130. } mytminfo;
  131.  
  132. mytminfo tmi;
  133. long del = 4000;
  134.     
  135.     // our delightful time manager task
  136. pascal void mytask(void)
  137. {
  138.     mytminfo *rec;
  139.     long oldA5;
  140.     long newdelay;
  141.     
  142.     ProcessSerialNumber psn;
  143.     OSErr err;
  144.     int i, newone;
  145.     Boolean res;
  146.     
  147.     
  148.         // get our data
  149.     asm {
  150.         move.l A1, rec\
  151.     }
  152.     setupcode();
  153.     oldA5 = SetA5(rec->tmRefCon);
  154.     
  155.     err = GetFrontProcess(&psn);
  156.     newone = false;
  157.     if (!err) {
  158.         newone = true;        // assume unique
  159.             // check if it's our app specially
  160.         err = SameProcess(&psn, &usPSN, &res);
  161.         if (!err && res)
  162.             newone = false;        // it's Cheat itself
  163.         else
  164.             for (i=0;i<numProcs;i++) {
  165.                 err = SameProcess(&psn, &InfoRec[i].processNumber, &res);
  166.                 if (!err && res)
  167.                     newone = false;        // already found
  168.             }
  169.         if (newone) {
  170.             // patch the trap
  171.             SetToolTrapAddress((long) newGetKeys, 0x176);
  172.         }
  173.     }
  174.     if (!newone) {        // install it again and wait
  175.         newdelay = 4000;
  176.         PrimeTime((QElemPtr) rec, newdelay);
  177.     }    // else we have patched one thing, that's enough for today
  178.     
  179.     cleanupcode();
  180. }
  181.  
  182.     // put the task in the queue
  183. void installTMTask(void)
  184. {
  185.     RememberA5();        // used in trap patch
  186.  
  187.     shouldclearup = false;
  188.     
  189.         // used for getting key stuff, needs to be changed eventually
  190.     oldtrap = GetToolTrapAddress(0x176);
  191.     verify(oldtrap);
  192.     
  193.     tmi.atm.tmAddr = (void *) mytask;
  194.     tmi.tmWakeUp = 0;
  195.     tmi.tmReserved = 0;
  196.     tmi.tmRefCon = SetCurrentA5();
  197.     InsTime((QElemPtr) &tmi);
  198.     PrimeTime((QElemPtr) &tmi, del);
  199. }
  200.  
  201. void removeTMTask(void)
  202. {
  203.     RmvTime((QElemPtr) &tmi);
  204. }